package com.vdm.action;

import com.vdm.model.SettlementInfo;
import com.vdm.model.UserInfo;
import com.vdm.model.VehicleInfo;
import com.vdm.service.SettlementService;
import com.vdm.service.VehicleService;
import com.vdm.util.Constants;
import com.vdm.util.ErrorConstants;
import com.vdm.util.ReturnMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/settlement")
public class SettlementController {

    @Autowired
    private VehicleService vehicleService;

    @Autowired
    private SettlementService settlementService;

    @RequestMapping("/toSettlementList")
    public String toSettlementList(Model model){
        List<VehicleInfo> vehicleInfos = vehicleService.getAllVehicleInfo();
        model.addAttribute("vehicleInfos",vehicleInfos);
        return "settlement/settlementList";
    }

    @ResponseBody
    @RequestMapping(value = "/settlementList",method = RequestMethod.GET)
    public Map<String,Object> settlementList(){
        List<SettlementInfo> userList = settlementService.settlementInfoList();
        int count = settlementService.getSettlementInfoCount();
        Map<String,Object> retMap = new HashMap();
        retMap.put("code",0);
        retMap.put("msg","");
        retMap.put("count",count);
        retMap.put("data",userList);
        return retMap;
    }

    @ResponseBody
    @RequestMapping(value = "/settlementListByCond",method = RequestMethod.GET)
    public Map<String,Object> settlementListByCond(String settlementTime,String vehicleNum,Integer page,Integer limit){
        //依据时间查出改时间段内需要的所有信息
        Map map = new HashMap();
        page=(page-1)*limit;//页面默认从1开始，，每页个数默认为10条
        map.put("page",page);//从第几页开始
        map.put("limit",limit);//每页显示多少条记录
        map.put("settlementTime",settlementTime);
        map.put("vehicleNum",vehicleNum);

        List<SettlementInfo> userList = settlementService.settlementListByCond(map);
        int count = settlementService.getSettlementListByCondCount(map);
        Map<String,Object> retMap = new HashMap();
        retMap.put("code",0);
        retMap.put("msg","");
        retMap.put("count",count);
        retMap.put("data",userList);
        return retMap;
    }

    @RequestMapping("/toAddSettlement")
    public String toAddSettlement(String settlementTime,String vehicleNum,Model model){
        //依据时间查出改时间段内需要的所有信息
        String startTime = settlementTime + "-01 00:00:01";
        String endTime = settlementTime + "-31 23:59:59";
        Map map = new HashMap();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("vehicleNum",vehicleNum);
        SettlementInfo settlement = settlementService.autoSettlement(map);
        if(settlement!=null && settlement.getVehicleNum()!=null){
            settlement.setCreateTime(new Date());
            settlement.setSettlement(settlementTime);
            model.addAttribute("settlement",settlement);
            return "settlement/addSettlement";
        }else{
            return "redirect:toSettlementList";
        }


    }

    @RequestMapping("/toEditSettlement")
    public String toEditSettlement(Integer id,Model model){
        SettlementInfo settlement = settlementService.selectByPrimaryKey(id);
        model.addAttribute("settlement",settlement);
        return "settlement/addSettlement";
    }


    @ResponseBody
    @RequestMapping(value = "/addSettlement" , method = RequestMethod.POST)
    public Map<String,Object> addSettlement(@RequestBody SettlementInfo settlement, HttpSession session){
        System.out.println("settlement:"+settlement);

        int count = 0;
        if(settlement.getId()==null) {
            UserInfo user = (UserInfo) session.getAttribute(Constants.CURRENT_USER);
            settlement.setCreateBy(user.getLoginName());
            settlement.setCreateTime(new Date());
            count = settlementService.insertSelective(settlement);
        }else{
            //更新
            count = settlementService.updateByPrimaryKeySelective(settlement);
        }

        if(count > 0) {
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }

    }
}
